package homeBean;

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;

import models.Usuario;
import dao.UsuarioDAO;
import Controller.SessionUtil;

public class Login {

	private String user;
	private String password;
	
	public Login (){
		
	}
	public String getUser() {
		return user;
	}
	public void setUser(String user) {
		this.user = user;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	
	public void Ingresar(ActionEvent ae){
		
		Usuario u = UsuarioDAO.findByLogin(user, password);
		
		if(u==null){
			SessionUtil.addErrorMessage("Usuario o Claves incorrectos");
		}
		else{
			SessionUtil.addSession(u.getId(), u.getNombres(), u.getApellidos(), u.getDni(), u.getCodigo(), u.getUser(), u.getCargo(), u.getArea().getId());
			SessionUtil.getFacesContext().addMessage(null, new FacesMessage(FacesMessage.FACES_MESSAGES,"Bienvenido "+ SessionUtil.getNombre()));
		}
		
		if(SessionUtil.getCargo().equalsIgnoreCase("Administrador")){
			SessionUtil.redirectTo("/administrador/welcome.xhtml");
		}
		else{
			SessionUtil.redirectTo("/user/welcome.xhtml");
		}
	}

}
